// 构造全局接口调用方法
//url method data   contentType
//loadingTxt
import {
	storage
} from "@/library/storage.js"
import { HTTP } from '../library/config';
export const myRequest = (params) => {
	return new Promise((resolve, reject) => {
		const defaultParams = {
			url: "",
			method: "POST",
			header: {},
			data: {},
			contentType: "application/json",
			token: uni.getStorageSync("token")?.token,
			appid: "wx93098259fa6b22f7",
			openid: uni.getStorageSync("userInfo")?.openid,
			version: "1.0",
		};
		const { url, method, header, data, contentType, token, appid, openid, version } = { ...defaultParams, ...params };
		if (!url) {
			resolve({
				code: 999,
				data: {},
				success: false,
				message: "url不能为空"
			});
			return;
		}
		uni.request({
			url: HTTP + url,
			method: method,
			header: {
				"Content-Type": contentType,
				"Version": version,
				"User-Token": token,
				"User-Appid": appid,
				"User-Openid": openid,
				...header
			},
			data: data,
			success: (res) => {
				console.log("request success....", res);
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					resolve({
						success: false,
						code: res.statusCode,
						data: res.data,
						message: res.message,
						timestamp: res.timestamp
					});
				}
			},
			fail: (err) => {
				console.log("request fail....", err);
				resolve({
					success: false,
					code: 999,
					data: {},
					message: "请求失败,请检查网络连接.",
					timestamp: Date.now()
				});
			}
		})
	})
};